import {request} from '@umijs/max';
import * as API from "@/services";
import {createRespResult} from "kf-web-library";

export async function queryMenus(params: API.MenuPageQueryParams): Promise<API.RespPageResult<API.Menu>> {
    return request('/api/v1/menu', {
        params,
    });
}

export async function queryMenuTree(params: API.MenuTreeQueryParams): Promise<API.RespListResult<API.Menu_TreeNode>> {
    return request('/api/v1/menu/tree', {
        params,
    });
}

export async function queryMenu(bizId: API.EntityId): Promise<API.RespResult<API.Menu>> {
    return request(`/api/v1/menu/${bizId}`);
}

export async function createMenu(entity: API.Menu_Create): Promise<API.RespResult<any>> {
    return request(`/api/v1/menu`, {
        method: 'POST',
        data: entity,
    });
}

export async function modifyMenu(entity: API.Menu_Modify): Promise<API.RespResult<any>> {
    return request(`/api/v1/menu/${entity.id}`, {
        method: 'PUT',
        data: entity,
    });
}

export async function patchMenu(entity: API.Menu_Patch): Promise<API.RespResult<any>> {
    return request(`/api/v1/menu/${entity.id}`, {
        method: 'PATCH',
        data: entity,
    });
}

export async function deleteMenu(bizId: API.EntityId): Promise<API.RespResult<any>> {
    return request(`/api/v1/menu/${bizId}`, {
        method: 'DELETE',
    });
}

export async function deleteMenus(bizIds: API.EntityId[]): Promise<API.RespResult<any>> {
    return request(`/api/v1/menu`, {
        method: 'DELETE',
        data: {
            ids: bizIds,
        },
    });
}

/**
 * 获取当前登录用户前端展示菜单
 * @param opts
 */
export async function queryFrontMenuTree(opts?: API.AxiosRequestOpts): Promise<API.RespResult<API.FrontMenu[]>> {
     return request<API.RespResult<API.FrontMenu[]>>('/api/v1/menu/fr-tree', {
         ...(opts || {}),
     });
}
